/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import util.filter.Predicate;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_professional_info_requeriment_and_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchProfessionalInfoRequerimentAndOperator.findAll", query = "SELECT s FROM SearchProfessionalInfoRequerimentAndOperator s"),
    @NamedQuery(name = "SearchProfessionalInfoRequerimentAndOperator.findByIdSearchProfessionalInfoRequerimentAndOperator", query = "SELECT s FROM SearchProfessionalInfoRequerimentAndOperator s WHERE s.idSearchProfessionalInfoRequerimentAndOperator = :idSearchProfessionalInfoRequerimentAndOperator")})
public class SearchProfessionalInfoRequerimentAndOperator implements Serializable, Predicate<Candidate> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_PROFESSIONAL_INFO_REQUERIMENT_AND_OPERATOR")
    private Integer idSearchProfessionalInfoRequerimentAndOperator;
    @JoinColumn(name = "JOB_POSITION", referencedColumnName = "ID_JOB_POSITION")
    @ManyToOne(fetch = FetchType.LAZY)
    private JobPosition jobPosition;
    @JoinColumn(name = "SECTOR", referencedColumnName = "ID_SECTOR")
    @ManyToOne(fetch = FetchType.LAZY)
    private Sector sector;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MINIMUM_JOB_DURATION", referencedColumnName = "ID_JOB_DURATION")
    @ManyToOne(fetch = FetchType.LAZY)
    private JobDuration minimumJobDuration;

    public SearchProfessionalInfoRequerimentAndOperator() {
    }

    public SearchProfessionalInfoRequerimentAndOperator(Integer idSearchProfessionalInfoRequerimentAndOperator) {
        this.idSearchProfessionalInfoRequerimentAndOperator = idSearchProfessionalInfoRequerimentAndOperator;
    }

    public Integer getIdSearchProfessionalInfoRequerimentAndOperator() {
        return idSearchProfessionalInfoRequerimentAndOperator;
    }

    public void setIdSearchProfessionalInfoRequerimentAndOperator(Integer idSearchProfessionalInfoRequerimentAndOperator) {
        this.idSearchProfessionalInfoRequerimentAndOperator = idSearchProfessionalInfoRequerimentAndOperator;
    }

    public JobPosition getJobPosition() {
        return jobPosition;
    }

    public void setJobPosition(JobPosition jobPosition) {
        this.jobPosition = jobPosition;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public JobDuration getMinimumJobDuration() {
        return minimumJobDuration;
    }

    public void setMinimumJobDuration(JobDuration minimumJobDuration) {
        this.minimumJobDuration = minimumJobDuration;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchProfessionalInfoRequerimentAndOperator != null ? idSearchProfessionalInfoRequerimentAndOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean sectorRes=false;
        boolean minimumJobDurationRes=false;
        boolean jobPositionRes=false;
        if (!(object instanceof SearchProfessionalInfoRequerimentAndOperator)) {
            return false;
        }
        SearchProfessionalInfoRequerimentAndOperator other = (SearchProfessionalInfoRequerimentAndOperator) object;
        
        //Sector test
        if(this.sector==null && other.getSector()==null){
            sectorRes=true;
        }
        else if(this.sector!=null && other.getSector()!=null){
            sectorRes=this.sector.equals(other.getSector());
        }
        //MinimumJobDuration test
        if(this.getMinimumJobDuration()==null && other.getMinimumJobDuration()==null){
            minimumJobDurationRes=true;
        }
        else if(this.getMinimumJobDuration()!=null && other.getMinimumJobDuration()!=null){
            minimumJobDurationRes=this.getMinimumJobDuration().equals(other.getMinimumJobDuration());
        }
        //JobPosition test
        if(this.getJobPosition()==null && other.getJobPosition()==null){
            jobPositionRes=true;
        }
        else if(this.getJobPosition()!=null && other.getJobPosition()!=null){
            jobPositionRes=this.getJobPosition().equals(other.getJobPosition());
        }
        Boolean res=sectorRes && minimumJobDurationRes && jobPositionRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchProfessionalInfoRequerimentAndOperator[ idSearchProfessionalInfoRequerimentAndOperator=" + idSearchProfessionalInfoRequerimentAndOperator + " ]";
    }

    @Override
    public boolean test(Candidate c) {
        boolean pass=false;
        if(c.getCandidateProfessionalInfoList()!=null){
            for(CandidateProfessionalInfo cpi: c.getCandidateProfessionalInfoList()){
                boolean validJobPosition=getJobPosition()==null;
                boolean validJobDuration=getMinimumJobDuration()==null;
                boolean validSector=getSector()==null;
                if(getJobPosition()!=null && cpi.getJobPosition()!=null){
                    validJobPosition=getJobPosition().equals(cpi.getJobPosition());
                }
                if(getMinimumJobDuration()!=null && cpi.getJobDuration()!=null){
                    validJobDuration=getMinimumJobDuration().getValue()<=cpi.getJobDuration().getValue();
                }
                if(getSector()!=null && cpi.getSector()!=null){
                    validSector=getSector().equals(cpi.getSector());
                }
                pass=validJobPosition && validJobDuration && validSector;
                if(pass){
                    break;
                }
            }
        }
        return pass;
    }
    
}
